package cate.game.farm.local.endless;

import cate.common.table.d.GDMail;
import cate.common.table.d.GDOperation;
import cate.game.framework.ModuleMan;
import cate.game.mail.po.MailBuilder;
import cate.game.rank.po.Rank;
import cate.game.res.MixRes;
import cate.game.role.mail.Mail;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class EndlessManager extends ModuleMan {
    private volatile boolean handling;
    @Override
    public void initDependent() throws Exception {

    }

    @Override
    public void onDaySpan() {
        if (handling) {
            return;
        }
        handling = true;
        List<Rank> ranks = game.rank.endless.getRankList(null);
        for (int i=0; i<ranks.size(); ++i) {
            String rewardStr = game.table.endless.rankReward.getReward(i);
            if (StringUtils.isNotBlank(rewardStr)) {
                List<Object> params = new ArrayList<>();
                params.add(i + 1);
                Mail mail = new MailBuilder()
                                .setReward(new MixRes(rewardStr))
                                .setContentId(GDMail.Tid.ENDLESS_RANK)
                                .setParamList(params)
                                .setOperation(GDOperation.ENDLESS_PASS_REWARD)
                                .setOperationSub(i + 1)
                                .build();
                game.mail.send(ranks.get(i).owner.uid, mail);
                log.info("无尽试炼排行榜第{}名是{}, value={}", i + 1, ranks.get(i).owner.base.name, ranks.get(i).value);
            }
        }
        game.rank.endless.clearList();
        handling = false;
    }

    public boolean canPlay() {
        return !handling;
    }
}
